import axios from 'axios';
import * as cheerio from 'cheerio';
import iconv from 'iconv-lite';
import fs from 'fs/promises';
import path from 'path';
import { convertTxtToEpub } from './converter.js';

const BASE_URL = 'https://www.xxyanqing.net';
const NOVEL_URL = 'https://www.xxyanqing.net/book/75643218/';
const START_CHAPTER = 200;

// 延迟函数
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));

// 获取页面内容
async function fetchPage(url) {
    try {
        const response = await axios.get(url, {
            responseType: 'arraybuffer',
            headers: {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
            }
        });
        return iconv.decode(response.data, 'gbk');
    } catch (error) {
        console.error(`Error fetching ${url}:`, error.message);
        return null;
    }
}

// 获取章节内容
async function getChapterContent(url) {
    const html = await fetchPage(url);
    if (!html) return null;

    const $ = cheerio.load(html);
    const content = $('#content').text().trim();
    return content;
}

// 主函数
async function main() {
    try {
        // 创建输出目录
        const outputDir = 'novel_output';
        await fs.mkdir(outputDir, { recursive: true });

        // 获取目录页面
        const html = await fetchPage(NOVEL_URL);
        if (!html) {
            console.error('Failed to fetch novel page');
            return;
        }

        const $ = cheerio.load(html);
        const chapters = [];

        // 找到"正文卷"对应的dt节点
        let foundZhengwen = false;
        $('.listmain dl').children().each((i, elem) => {
            if (elem.tagName === 'dt' && $(elem).text().includes('正文卷')) {
                foundZhengwen = true;
                return;
            }
            if (foundZhengwen && elem.tagName === 'dd') {
                const a = $(elem).find('a');
                const href = a.attr('href');
                const title = a.text().trim();
                if (href && title) {
                    chapters.push({
                        title,
                        url: BASE_URL + href
                    });
                }
            }
        });

        // 只处理第200章之后的内容
        const targetChapters = chapters.slice(START_CHAPTER - 1);

        console.log(`Found ${targetChapters.length} chapters to download`);

        // 获取书名
        const bookTitle = $('h2').first().text().trim() || 'novel';
        const outputFile = path.join(outputDir, `${bookTitle}.txt`);
        
        // 先清空文件
        await fs.writeFile(outputFile, '', 'utf8');

        // 下载每个章节
        for (let i = 0; i < targetChapters.length; i++) {
            const chapter = targetChapters[i];
            console.log(`Downloading chapter ${i + 1}/${targetChapters.length}: ${chapter.title}`);

            const content = await getChapterContent(chapter.url);
            if (content) {
                // 去除广告脚本和提示
                let cleanContent = content
                    .replace(/app2\(\);/g, '')
                    .replace(/chaptererror\(\);/g, '')
                    .replace(/天才一秒记住本站地址.*?m\.xxyanqing\.cc/g, '')
                    .replace(/https:\/\/www\.xxyanqing\.cc\/book\/\d+\/\d+\.html/g, '')
                    .replace(/\n{2,}/g, '\n\n')
                    .trim();

                const chapterText = `\n${chapter.title}\n\n${cleanContent}\n`;
                await fs.appendFile(outputFile, chapterText, 'utf8');
                console.log(`Chapter ${i + 1} saved to file`);
            }

            // 添加延迟以避免请求过快
            await delay(1000);
        }

        console.log(`Download completed! All chapters saved to ${outputFile}`);
        
        // 自动转换为 EPUB
        console.log('开始转换为 EPUB 格式...');
        await convertTxtToEpub(outputFile);
        console.log('转换完成！');
    } catch (error) {
        console.error('Error:', error);
    }
}

main(); 